import 'package:flutter/material.dart';
import 'package:flutter_app_demo/base/color/common_color.dart';

/// @author wangliang at 2021/11/10

class GradientButton extends StatefulWidget {
  final Function onTap;
  final bool disable;
  final Gradient? gradient;
  final double? width;
  final double? height;
  final double? bgRadius;
  final Widget child;

  const GradientButton(
      {Key? key,
      required this.onTap,
      required this.child,
      this.disable = false,
      this.gradient,
      this.width,
      this.height,
      this.bgRadius})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _GradientButtonState();
  }
}

class _GradientButtonState extends State<GradientButton> {
  var _pressed = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      child: AnimatedContainer(
        width: widget.width ?? double.infinity,
        height: widget.height ?? 44,
        alignment: Alignment.center,
        duration: const Duration(microseconds: 500),
        foregroundDecoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(widget.bgRadius ?? 44)),
          color: widget.disable ? Colors.transparent : (_pressed ? Colors.white.withOpacity(0.5) : Colors.transparent),
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(widget.bgRadius ?? 44)),
          gradient: widget.gradient ??
              LinearGradient(
                begin: Alignment.centerLeft,
                end: Alignment.centerRight,
                colors: [
                  widget.disable ? CommonColor.std007 : const Color(0xffF0CC76),
                  widget.disable ? CommonColor.std007 : const Color(0xffEEB961)
                ],
              ),
        ),
        child: widget.child,
      ),
      onTap: () {
        if (!widget.disable) {
          widget.onTap();
        }
      },
      onTapDown: (d) => setState(() => _pressed = true),
      onTapUp: (d) => setState(() => _pressed = false),
      onTapCancel: () => setState(() => _pressed = false),
    );
  }
}
